package com.parleys.activity;

import java.util.List;

import roboguice.inject.InjectView;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.app.SearchManager;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;

import com.google.inject.Inject;
import com.parleys.R;
import com.parleys.adapter.ParleysListAdapter;
import com.parleys.androidservice.ParleysMediaPlayer;
import com.parleys.domain.Channel;
import com.parleys.domain.ParleysListObject;
import com.parleys.domain.Presentation;
import com.parleys.domain.Space;
import com.parleys.exception.WebserviceNotAvailableException;
import com.parleys.helper.ConnectionErrorRunnable;
import com.parleys.helper.ParleysLoadingDialog;
import com.parleys.helper.ProcessResultListRunnable;
import com.parleys.service.intf.ParleysService;
import com.parleys.service.intf.PresentationAssetsService;

public class SearchActivity extends AbstractParleysActivity {

	private static final Space NULL_SPACE = new Space();
	private static final Channel NULL_CHANNEL = new Channel();

	@InjectView(R.id.list)
	protected ListView listView;

	@InjectView(R.id.emptyText)
	private TextView emptyText;

	@Inject
	private ParleysService parleysService;

	@Inject
	private PresentationAssetsService presentationAssetsService;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.parleys_list);

		emptyText.setText(getString(R.string.no_search_results_found));

		final Intent intent = getIntent();

		final String query = intent.getStringExtra(SearchManager.QUERY);
		setTitle(getString(R.string.search_results_found_for, query));

		listView.setOnItemClickListener(new PresentationOnClickHandler());
		listView.setOnItemLongClickListener(new PresentationOnLongClickHandler());

		final ProgressDialog progressDialog = new ParleysLoadingDialog(this);
		progressDialog.show();

		final PresentationAssetsService presentationAssetsService = this.presentationAssetsService;
		final ParleysService parleysService = this.parleysService;

		/**
		 * Offloading the fetching of the list data to a different thread
		 */
		Runnable fillList = new Runnable() {

			@Override
			public void run() {
				try {
					List<? extends ParleysListObject> list = parleysService.searchDownloadedPresentations(query);
					ParleysListAdapter parleysListAdapter = new ParleysListAdapter(SearchActivity.this, list, "",
							presentationAssetsService);

					SearchActivity.this.runOnUiThread(new ProcessResultListRunnable(list, listView, emptyText,
							parleysListAdapter, progressDialog));
				} catch (WebserviceNotAvailableException e) {
					SearchActivity.this.runOnUiThread(new ConnectionErrorRunnable(SearchActivity.this, progressDialog));
				}

			}
		};

		new Thread(fillList).start();

	}

	private class PresentationOnClickHandler implements OnItemClickListener {

		@Override
		public void onItemClick(AdapterView<?> listView, View layout, int arg2, long arg3) {
			Presentation presentation = (Presentation) ((ParleysListAdapter) listView.getAdapter()).getItem(arg2);
			gotoDetailActivity(NULL_SPACE, NULL_CHANNEL, presentation, false);
		}

	}

	private class PresentationOnLongClickHandler implements OnItemLongClickListener {

		@Override
		public boolean onItemLongClick(final AdapterView<?> listView, final View viewClicked, final int position,
				final long rowId) {
			final Presentation presentation = (Presentation) ((ParleysListAdapter) listView.getAdapter())
					.getItem(position);

			final CharSequence offlineItems[] = { getString(R.string.general_info), getString(R.string.chapters),
					getString(R.string.slides), getString(R.string.play) };
			CharSequence items[];

			items = offlineItems;

			AlertDialog.Builder builder = new AlertDialog.Builder(SearchActivity.this);
			builder.setTitle(presentation.getName());
			builder.setCancelable(true);
			builder.setItems(items, new DialogInterface.OnClickListener() {
				public void onClick(final DialogInterface dialog, final int item) {
					if (item == 0) {
						gotoDetailActivity(NULL_SPACE, NULL_CHANNEL, presentation, false);
					} else if (item == 1) {
						gotoChaptersActivity(NULL_SPACE, NULL_CHANNEL, presentation, false);
					} else if (item == 2) {
						gotoSlidesActivity(NULL_SPACE, NULL_CHANNEL, presentation, false);
					} else if (item == 3) {
						ParleysMediaPlayer.toggle(NULL_CHANNEL, presentation, false);
						gotoSlidesActivity(NULL_SPACE, NULL_CHANNEL, presentation, false);
					}
				}
			});

			builder.create().show();

			return true;
		}

	}

	@Override
	protected void onResume() {
		super.onResume();
		ParleysMediaPlayer.stop();
	}

}
